/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minidota.model;

import com.minidota.utils.Util;

/**
 *
 * @author roman
 */
public class Arrow extends BaseObject {

    BaseObject fromUser;
    BaseObject toUser;
    private int moveSpeed = 150;

    public Arrow(BaseObject fromUser, BaseObject toUser) {
        this(fromUser, toUser, -20, 130);
    }

    public Arrow(BaseObject fromUser, BaseObject toUser, int offsetDX, int offsetDY) {
        this.fromUser = fromUser;
        this.toUser = toUser;
        if (fromUser instanceof User) {
            setRoom(((User) fromUser).getRoom());
            this.moveSpeed = ((User) fromUser).getBasicHero().getArrowSpeed();
        } else if (fromUser instanceof Tower) {
            setRoom(((Tower) fromUser).getRoom());
            this.moveSpeed = 100;
        }
        setGameId(getRoom().getFreeRocketId());
        int offsetX = offsetDX;
        int offsetY = offsetDY;
        Vector2 pos = new Vector2(fromUser.getPosition().getX() + offsetX * Util.sin(fromUser.getViewAngle()) + offsetY * Util.cos(fromUser.getViewAngle()), fromUser.getPosition().getY() - offsetX * Util.cos(fromUser.getViewAngle()) + offsetY * Util.sin(fromUser.getViewAngle()));
        this.setPosition(pos);
        setViewAngle(toUser.getPosition().sub(fromUser.getPosition()).calcVector2Angle());
    }

    @Override
    public void updateData(long tick) {
        super.updateData(tick);
        if (toUser != null && toUser.isAlive()) {
            if (getPosition().sub(toUser.getPosition()).lengthSquared() < toUser.getRadius() * toUser.getRadius()) {
                toUser.doDamage(fromUser, fromUser.getDamage());
                getRoom().getArrows().remove(this);
            }
            Vector2 speed = toUser.getPosition().sub(getPosition()).toLength(moveSpeed);
            Vector2 newPosition = getPosition().add(speed);
            if (newPosition.sub(toUser.getPosition()).lengthSquared() > moveSpeed * moveSpeed) {
                setPosition(newPosition);
            } else {
                setPosition(toUser.getPosition());
            }
            setViewAngle(speed.calcVector2Angle());
        } else {
            getRoom().getArrows().remove(this);
        }
    }

    @Override
    public String getSendData() {
        StringBuilder sb = new StringBuilder();
        sb.append(Util.getCharByCode(getGameId()));
        sb.append(Util.getCharByCode((int) Math.round(getPosition().getX() * 5)));
        sb.append(Util.getCharByCode((int) Math.round(getPosition().getY() * 5)));
        sb.append(Util.getCharByCode(getViewAngle()));
        sb.append(Util.getCharByCode(this.fromUser.getGameId()));
        return sb.toString();
    }

    public void init() {
        getRoom().getArrows().add(this);
    }

    /**
     * @return the moveSpeed
     */
    public int getMoveSpeed() {
        return moveSpeed;
    }

    /**
     * @param moveSpeed the moveSpeed to set
     */
    public void setMoveSpeed(int moveSpeed) {
        this.moveSpeed = moveSpeed;
    }
}
